package com.authManager.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 说明：
 * 
 * @author: 小旋风
 */
@Component
public class BaseInterceptor implements HandlerInterceptor {

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// 在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行，主要是用于进行资源清理工作
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// 请求处理之后进行调用，但是在视图被渲染之前，即Controller方法调用之后
	}
	@Value("${server.servlet.context-path}")
	private String basePath;
	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		request.setAttribute("basePath", basePath);
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		// 这里填写你允许进行跨域的主机ip
		httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
		// 加入支持自定义的header 加入元素 token 目前没用到token和source
		response.setHeader("Access-Control-Allow-Headers", "authManager-token,authManager-source"); 
		response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
			
		return true;
	}

}